package com.qiche.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.qiche.service.CarBrandService;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.mapper.CarBrandMapper;
import com.qiche.mapper.CarFactoryMapper;
import com.qiche.mapper.CarLineMapper;
import com.qiche.mapper.CarModelMapper;
import com.qiche.mapper.CarYearMapper;
import com.qiche.model.CarBrand;
import com.qiche.model.CarFactory;
import com.qiche.model.CarLine;
import com.qiche.model.CarModel;
import com.qiche.model.CarYear;

import tk.mybatis.mapper.entity.Example;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("carBrandService")
public class CarBrandServiceImpl extends BaseService<CarBrand> implements CarBrandService {

	@Resource
	private CarBrandMapper mapper;
	@Resource
	private CarLineMapper lineMapper;
	@Resource
	private CarFactoryMapper factoryMapper;

	@Resource
	private CarYearMapper yearMapper;
	
	@Resource
	private CarModelMapper modelMapper;


	@Override
	public PageInfo<CarBrand> selectByPage(CarBrand brand, int start, int length) {
		int page = start / length + 1;
		Example example = new Example(CarBrand.class);
		Example.Criteria criteria = example.createCriteria();
		// 分页查询
		PageHelper.startPage(page, length);
		List<CarBrand> regionList = selectByExample(example);


		return new PageInfo<>(regionList);
	}

	@Override
	public CarBrand selectById(Long brandId) {
		Example example = new Example(CarBrand.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", brandId);
		List<CarBrand> regionList = selectByExample(example);
		if (regionList.size() > 0) {
			return regionList.get(0);
		}
		return null;
	}

	@Override
	public List<CarBrand> GetPopularBrands() {
		Example example = new Example(CarBrand.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("isPopular", 1);
		List<CarBrand> brandList = selectByExample(example);
		return brandList;
	}

	@Override
	public List<CarBrand> selectAll() {
		return mapper.selectAllWithVehicles();
	}

	@Override
	public List<CarBrand> GetBrandByFirstLetter(String firstLetter) {
		Example example = new Example(CarBrand.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("classification", firstLetter);
		List<CarBrand> brandList = selectByExample(example);
		return brandList;
	}

	@Override
	public List<CarLine> GetLinesByBrandId(Long brandId) {
		/*
		 * List<CarFactory> factoryList=GetFactorysByBrandId(brandId); List <Long>
		 * factoryIds = factoryList.stream().map(factory ->
		 * factory.getFactoryId()).collect(Collectors.toList());
		 */
		Example example = new Example(CarLine.class);
		Example.Criteria criteria = example.createCriteria();
		/* criteria.andIn("factoryId", factoryIds); */
		criteria.andEqualTo("brandId", brandId);
		List<CarLine> lineList = lineMapper.selectByExample(example);
		return lineList;
	}

	@Override
	public List<CarFactory> GetFactorysByBrandId(Long brandId) {
		Example example = new Example(CarFactory.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("brandId", brandId);
		List<CarFactory> factoryList = factoryMapper.selectByExample(example);
		return factoryList;
	}

	@Override
	public List<CarYear> GetYearsByLineId(Long lineId) {
		Example example = new Example(CarYear.class);
		Example.Criteria criteria = example.createCriteria();
		/* criteria.andIn("factoryId", factoryIds); */
		criteria.andEqualTo("lineId", lineId);
		List<CarYear> yearList = yearMapper.selectByExample(example);
		return yearList;
	}

	@Override
	public List<CarModel> GetModelsByYear(Long lineId, String year) {
		Example example = new Example(CarModel.class);
		Example.Criteria criteria = example.createCriteria();
		/* criteria.andIn("factoryId", factoryIds); */
		criteria.andEqualTo("lineId", lineId);
		criteria.andEqualTo("year", year);
		List<CarModel> modelList = modelMapper.selectByExample(example);
		return modelList;
	}

	/*@Override
	public CarModel GetModelsByName(String modelName) {
		Example example = new Example(CarModel.class);
		Example.Criteria criteria = example.createCriteria();
		*//* criteria.andIn("factoryId", factoryIds); *//*
		criteria.andEqualTo("lineId", lineId);
		criteria.andEqualTo("year", year);
		List<CarModel> modelList = modelMapper.selectByExample(example);
		return modelList;
	}*/

}
